package com.cmlite.database;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.cmlite.model.Profile;



public class DBManager {
	// Database fields
	private SQLiteDatabase database;
	private MySQLiteHelper dbHelper;
	private String[] allColumns = { MySQLiteHelper.COLUMN_ID,
			MySQLiteHelper.COLUMN_EMAIL, MySQLiteHelper.COLUMN_FULL_NAME,
			MySQLiteHelper.COLUMN_FIRST_NAME, MySQLiteHelper.COLUMN_LAST_NAME,
			MySQLiteHelper.COLUMN_PHONE, MySQLiteHelper.COLUMN_PHONE_OTHER,
			MySQLiteHelper.COLUMN_ADDRESS, MySQLiteHelper.COLUMN_ADDRESS_OTHER,
			MySQLiteHelper.COLUMN_GENDER, MySQLiteHelper.COLUMN_BIRTHDAY };

	public DBManager(Context context) {
		dbHelper = new MySQLiteHelper(context);
	}

	public void open() throws SQLException {
		database = dbHelper.getWritableDatabase();
	}

	public void close() {
		dbHelper.close();
	}

	public long createProfile(Profile profile) {
		try {
			ContentValues cv = new ContentValues();
			cv.put(MySQLiteHelper.COLUMN_ID, profile.getId());
			cv.put(MySQLiteHelper.COLUMN_EMAIL, profile.getEmail());
			cv.put(MySQLiteHelper.COLUMN_FIRST_NAME, profile.getFirst_name());
			cv.put(MySQLiteHelper.COLUMN_LAST_NAME, profile.getLast_name());
			return database.insert(MySQLiteHelper.TABLE_PROFILE, null, cv);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return -1;
	}

	// public OutProfile createProfile(OutProfile profile) {
	// ContentValues values = new ContentValues();
	// values.put(MySQLiteHelper.COLUMN_EMAIL, profile.getEmail());
	// long insertId = database.insert(MySQLiteHelper.TABLE_PROFILE, null,
	// values);
	// Cursor cursor = database.query(MySQLiteHelper.TABLE_PROFILE,
	// allColumns, MySQLiteHelper.COLUMN_ID + " = " + insertId, null,
	// null, null, null);
	// cursor.moveToFirst();
	// OutProfile bProfile = cursorToProfile(cursor);
	// cursor.close();
	// return bProfile;
	// }

	public boolean checkEmail(String email) {
		try {
			Cursor cursor = database.query(MySQLiteHelper.TABLE_PROFILE,
					new String[] { MySQLiteHelper.COLUMN_EMAIL },
					MySQLiteHelper.COLUMN_EMAIL + " = '" + email + "'", null,
					null, null, null);
			if (cursor != null && cursor.getCount() > 0) {
				cursor.moveToFirst();
				final String tempEmail = cursor.getString(0);
				cursor.close();
				if (tempEmail != null && email != null
						&& tempEmail.equalsIgnoreCase(email))
					return true;
				return false;
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return false;
	}

	public void deleteProfile(Profile profile) {
		long id = profile.getId();
		System.out.println("Comment deleted with id: " + id);
		database.delete(MySQLiteHelper.TABLE_PROFILE, MySQLiteHelper.COLUMN_ID
				+ " = " + id, null);
	}

	public List<Profile> getAllProfile() {
		List<Profile> profiles = new ArrayList<Profile>();

		Cursor cursor = database.query(MySQLiteHelper.TABLE_PROFILE,
				allColumns, null, null, null, null, null);

		cursor.moveToFirst();
		while (!cursor.isAfterLast()) {
			Profile profile = cursorToProfile(cursor);
			profiles.add(profile);
			cursor.moveToNext();
		}
		// Make sure to close the cursor
		cursor.close();
		return profiles;
	}

	public Profile getProfile(String email) {
		Profile profile = null;
		try {
			Cursor cursor = database.query(MySQLiteHelper.TABLE_PROFILE,
					allColumns, MySQLiteHelper.COLUMN_EMAIL + " = '" + email
							+ "'", null, null, null, null);

			cursor.moveToFirst();
			profile = cursorToProfile(cursor);
			// Make sure to close the cursor
			cursor.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return profile;
	}

	private Profile cursorToProfile(Cursor cursor) {

		Profile profile = new Profile();
		profile.setEmail(cursor.getString(1));
		profile.setFirst_name(cursor.getString(3));
		profile.setLast_name(cursor.getString(4));
		return profile;
	}

	public boolean updateProfile(Profile profile) {
		try {
			ContentValues cv = new ContentValues();
			cv.put(MySQLiteHelper.COLUMN_ID, profile.getId());
			cv.put(MySQLiteHelper.COLUMN_EMAIL, profile.getEmail());
			cv.put(MySQLiteHelper.COLUMN_FIRST_NAME, profile.getFirst_name());
			cv.put(MySQLiteHelper.COLUMN_LAST_NAME, profile.getLast_name());
			return this.database.update(MySQLiteHelper.TABLE_PROFILE, cv,
					MySQLiteHelper.COLUMN_ID + " = " + profile.getId()
							+ " AND " + MySQLiteHelper.COLUMN_EMAIL + " = '"
							+ profile.getEmail() + "'", null) > 0;
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return false;
	}
}
